package com.xsmart.oa.services;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.QueryAdminMenuDAO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by lin.wenhui on 2018/1/9.
 */
@Service
public class AdminMenuService {
    @Autowired
    QueryAdminMenuDAO queryAdminMenuDAO;

    /**
     * 获取用户菜单
     * @param userId
     * @return
     */
    public  List<Map<String, Object>> queryAdminMenu(String userId) {
        List<Map<String, Object>> menuList=null;
        if(userId!=null){
            menuList = queryAdminMenuDAO.queryMenu(userId, 1);
//        }else{
//            menuList=queryAdminMenuDAO.queryAllMenu(1);
        }
        //获取子节点
        getMenuNodeChildren(menuList, userId);
//        for(int i=0;i<menuList.size();i++){
//            Map<String, Object> firstNode = menuList.get(i);
//            firstNode.put("layout",MapUtils.getString(firstNode,"path"));
//            firstNode.remove("path");
//        }
       // responseBean.addResultItem("adminMenu", menuList);
        return menuList;
    }

    /**
     * 获取子节点
     * @param list
     * @param userId
     */
    private void getMenuNodeChildren(List<Map<String, Object>> list, String userId) {
        Iterator<Map<String, Object>> itList = list.listIterator();
        List<Map<String, Object>> childrenList=null;
        while (itList.hasNext()) {
            Map<String, Object> menu = itList.next();
            Integer menuId = MapUtils.getInteger(menu, "menu_id");
            menu.remove("menu_id");
            menu.remove("pmenu_id");
            if(userId!=null){
                childrenList = queryAdminMenuDAO.queryMenu(userId, menuId);
//            }else{
//                childrenList = queryAdminMenuDAO.queryAllMenu(menuId);
            }
            if (childrenList.size() > 0) {
                menu.put("children", childrenList);
                getMenuNodeChildren(childrenList, userId);
            }
        }
    }

    /**
     * 获取管理员所拥有的按钮权限
     * @param requestBean
     * @return
     */
    public ResponseBean queryAdminButtonPower(RequestBean requestBean) {
        String userId = requestBean.getRequestParamString("userId");
        String actionCode = requestBean.getRequestParamString("actionCode");
        int buttonPower = queryAdminMenuDAO.queryAdminButtonPower(userId, actionCode);
        ResponseBean responseBean = requestBean.getResponseBean();
        if (buttonPower > 0) {
            responseBean.addResultItem("adminButtonPower", true);
        } else {
            responseBean.addResultItem("adminButtonPower", false);
        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
